<?php
namespace App\Controller;
use Common\Controller\AppcommController;
class CarController extends AppcommController{

	protected $lastindex = 0;
	protected $maxnum = 0;
	protected $citys = 0;
	protected $ordertype = 0;
	protected $keybr = 0;
	protected $keyse = '';
	protected $keypr = 0;
	protected $keymo = 0;
	protected $keyta = 0;
	protected $keyag = 0;
	protected $keylo = 0;
	protected $keydi = 0;
	protected $keyco = 0;
	protected $keyen = 0;
	protected $keysea = 0;
	protected $keyoi = 0;
	protected $keycon = 0;
	protected $keywat = 0;
	protected $keyke = 0;
	protected $cityormakert = 0;

	public function listParam()
	{
		$this->lastindex = $_GET['pa'];
		if(empty($this->lastindex))
		{
			$this->lastindex = 0;
		}
		else
		{
			$total = $_GET['total'];		
		}
		$this->maxnum = $_GET['ma'];
		if(empty($this->maxnum))
			$this->maxnum = 40;

		/*城市/市场过滤条件*/
		$this->citys = $_GET['pl'];
		$this->ordertype = $_GET['or'];
		/*搜索品牌条件*/
		$this->keybr = $_GET['br'];
		/*搜索车系条件*/
		$this->keyse = $_GET['se'];
		/*搜索报价条件*/
		$this->keypr = $_GET['pr'];
		/*搜索箱型条件*/
		$this->keymo = $_GET['mo'];
		/*搜索标签条件*/
		$this->keyta = $_GET['ta'];
		/*搜索车龄条件*/
		$this->keyag = $_GET['ag'];
		/*搜索里程条件*/
		$this->keylo = $_GET['lo'];
		/*搜索排量条件*/
		$this->keydi = $_GET['di'];
		/*排放标准过滤条件*/
		$this->keyno = $_GET['no'];
		/*颜色过滤条件*/
		$this->keyco = $_GET['co'];
		/*变速箱过滤条件*/
		$this->keyen = $_GET['en'];
		/*座位过滤条件*/
		$this->keysea = $_GET['sea'];
		/*燃油过滤条件*/
		$this->keyoi = $_GET['oi'];
		/*国别过滤条件*/
		$this->keycon = $_GET['con'];
		/*是否急售*/
		$this->keywat = $_GET['wat'];
		/*搜索关键词条件*/
		$this->keyke = $_GET['ke'];
		/*选择的城市/市场*/
		$this->cityormakert = $_GET['ci'];
		$keybig = $_GET['big'];
		$keybid = $_GET['bid'];
		cookie('or',$this->ordertype);cookie('br',$this->keybr);cookie('se',$this->keyse);
		cookie('pr',$this->keypr);cookie('mo',$this->keymo);cookie('ta',$this->keyta);
		cookie('ag',$this->keyag);cookie('lo',$this->keylo);cookie('di',$this->keydi);
		cookie('no',$this->keyno);cookie('co',$this->keyco);cookie('en',$this->keyen);
		cookie('sea',$this->keysea);cookie('oi',$this->keyoi);cookie('con',$this->keycon);
		cookie('wat',$this->keywat);cookie('ke',$this->keyke);cookie('big',$this->keybig);cookie('bid',$this->keybid);
	}

	/**
		列表
	*/
	public function getlist()
	{
		$this->listParam();
		$carModel = D('Common/Cars');
		$result = $carModel->car_list($this->lastindex,$this->maxnum,$this->citys,$this->ordertype,$this->keybr,$this->keyse,$this->keypr,$this->keymo,$this->keyta,$this->keyag,$this->keylo,$this->keydi,$this->keyno,$this->keyco,$this->keyen,$this->keysea,$this->keyoi,$this->keycon,$this->keywat,$this->keyke,$this->cityormakert);
		
		if($result["Ret"] != "0")
		{
			echo $result["Msg"];
			return;
		}
		if($this->lastindex == 0)
		{
			$total = $result["Msg"];
		}
		cookie('total',$total);
		$showreset = "1";
		$showke = $this->keyke;
		if(mb_strlen($showke, 'utf8') > 5) 
		$showke = mb_substr($showke, 0, 5, 'utf8').'...';
		session('mycity',$this->citys);
		session('citype',$this->cityormakert);
		$keysearchlog = cookie('keysearchlog');
		$this->assign ( "keysearchlog", $keysearchlog );
		$this->assign("maxcount",$result["Msg"]);
		$datalist = $result["Data"];
		// echo "<pre>";print_r($datalist);exit;
		$this->assign ( "datalist", $datalist );
		$datacount = count($datalist);
		$this->assign ( "datacount", $datacount );
		$showpr = "";
		$showprmin = "";
		$showprmax = "";
		if(!empty($this->keypr))
		{
			$showpr = str_replace("<","小于",$this->keypr);
			$showpr = str_replace(">","大于",$showpr);
			$showpr .= '万';
			
			if(false !== stripos($this->keypr,"<")) { 
				$showprmin = "";
				$showprmax = substr($this->keypr,1);
			}
			if(false !== stripos($this->keypr,">")) { 
				$showprmax = "";
				$showprmin = substr($this->keypr,1);
			}
			if(false !== stripos($this->keypr,"-")) { 
				$pos =  stripos($this->keypr,"-");
				$showprmin = substr($this->keypr,0,$pos);
				$showprmax = substr($this->keypr,$pos + 1);
			}
		}
		$this->assign ( "showprmin", $showprmin );
		$this->assign ( "showprmax", $showprmax );
		$this->assign ( "showpr", $showpr );
		
		$showage = "";
		if(!empty($this->keyag ))
		{
			$showage = str_replace("<","小于",$this->keyag);
			$showage = str_replace(">","大于",$showage);
			$showage .= '年';
		}
		$this->assign ( "showage", $showage );
		
		$showlong = "";
		if(!empty($this->keylo ))
		{
			$showlong = str_replace("<","小于",$this->keylo);
			$showlong = str_replace(">","大于",$showlong);
			$showlong .= '万公里';
		}
		$this->assign ( "showlong", $showlong );
		$this->assign ( "showke", $showke );
		
		
		$showdi = "";
		if(!empty($this->keydi ))
		{
			$showdi = str_replace("<","小于",$this->keydi);
			$showdi = str_replace(">","大于",$showdi);
			$showdi .= 'L';
		}
		
		
		$this->assign ( "showdi", $showdi );
		
		if($this->keysea =='2-2')
		{
			$selectsea='2座';
		}
		elseif($this->keysea =='4-4')
		{
			$selectsea='4座';
		}
		elseif($this->keysea =='5-5')
		{
			$selectsea='5座';
		}
		elseif($this->keysea =='6-6')
		{
			$selectsea='6座';
		}
		elseif($this->keysea=='>7')
		{
			$selectsea='7座以上';
		}
		else
		{
			$titlesea =$this->keysea.'座';
		}
		$this->assign ( "titlesea", $selectsea );
		$rowcount =ceil($total/40);
		$this->assign ( "rowcount", $rowcount );
		if(  empty($this->keybr) && empty($this->keyse) &&
			empty($this->showpr) && empty($this->keymo) &&
			empty($this->keyta) && empty($this->keyen) &&
			empty($this->keyag) && empty($this->keylo) &&
			empty($this->keyoi) && empty($this->keycon) &&
			empty($this->keywat) && empty($this->keysea) && empty($this->showke) )
		{
			$showreset = "0";
		}
		$this->assign ( "showreset", $showreset );
		$emptylist = "<a href='tel:400-100-1313'>更多最新车源信息联系400-100-1313</a>";
		$this->assign ( "emptylist", $emptylist );
		
		$this->assign("getData",$_GET);
		$this->display ();
	}


	public function listbrand()
	{
		$this->get2model();

		//品牌
		$carModel = M('Cars');
		$pinpai = $carModel->field("CarBrand,CarK")->group('CarBrand')->select();
		$zm = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
		$arr = array();
		foreach ($pinpai as $key => $val) {
			if(in_array($val['cark'], $zm)){
					// $arr[$val['cark']]['name'] = $val['cark'];
					$arr[$val['cark']][] = $val['carbrand'];
			}else{
				$arr['其他'][] = $val['carbrand'];
			}
		}
		ksort($arr);
		// echo "<pre>";print_r($arr);exit;
		$this->assign('pinpai',$arr);
		// $curl = curl_init();
		// $url = "http://125.67.237.182:8000/public/uploads/js/brandwithkey.json";
		// curl_setopt($curl, CURLOPT_URL,$url);
		// curl_setopt($curl, CURLOPT_HEADER, 0);
		// curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		// $data = curl_exec($curl);
		// curl_close($curl);
		// $databds=json_decode( trim($data,chr(239).chr(187).chr(191)),true);
		// //var_dump($databds);
		// $this->assign ( "databds", $databds['Data'] );
		$this->display ();
	}
	public function listfilter()
	{
		// $this->get2model();
		$this->display ();
	}
	function get2model()
	{
		$this->listParam();
		$showage = "";
		$showagemin = "0";
		$showagemax = "8";
		if(!empty($this->keyag ))
		{
			if(false !== stripos($this->keyag,"<")) { 
				$showagemin = "0";
				$showagemax = substr($this->keyag,1);
			}
			if(false !== stripos($this->keyag,">")) { 
				$showagemax = "8";
				$showagemin = substr($this->keyag,1);
			}
			if(false !== stripos($this->keyag,"-")) { 
				$pos = stripos($this->keyag,"-");
				$showagemin = substr($this->keyag,0,$pos);
				$showagemax = substr($this->keyag,$pos + 1);
			}
		}
		$this->assign ( "showagemin", $showagemin );
		$this->assign ( "showagemax", $showagemax );
		
		$showlongmin = "0";
		$showlongmax = "15";
		if(!empty($this->keylo ))
		{
			if(false !== stripos($this->keylo,"<")) { 
				$showlongmin = "0";
				$showlongmax = substr($this->keylo,1);
			}
			if(false !== stripos($this->keylo,">")) { 
				$showlongmax = "15";
				$showlongmin = substr($this->keylo,1);
			}
			if(false !== stripos($this->keylo,"-")) { 
				$pos = stripos($this->keylo,"-");
				$showlongmin = substr($this->keylo,0,$pos);
				$showlongmax = substr($this->keylo,$pos + 1);
			}
		}
		$this->assign ( "showlongmin", $showlongmin );
		$this->assign ( "showlongmax", $showlongmax );
		
		$showdimin = "0";
		$showdimax = "5";
		if(!empty($this->keydi ))
		{
			if(false !== stripos($this->keydi,"<")) { 
				$showdimin = "0";
				$showdimax = substr($this->keydi,1);
			}
			if(false !== stripos($this->keydi,">")) { 
				$showdimax = "15";
				$showdimin = substr($this->keydi,1);
			}
			if(false !== stripos($this->keydi,"-")) { 
				$pos = stripos($this->keydi,"-");
				$showdimin = substr($this->keydi,0,$pos);
				$showdimax = substr($this->keydi,$pos + 1);
			}
		}
		$this->assign ( "showdimin", $showdimin );
		$this->assign ( "showdimax", $showdimax );
	}
	public function detail()
	{
		$carid = $_GET["id"];
		$car = $this->getcar($carid);
		if(empty($car))
		{
			echo "车辆错误";
			return;
		}
		$imgcount = count(explode(',',$car['imgs']));
		$imgnames = explode(',',$car['imgnames']);
		$names = array("正侧","正前","前排","后排",
			"中控","发动机舱","其他");
		for($i = 0;$i < $imgcount;$i++)
		{
			$index = $imgnames[$i] - 1;
			$imgnames[$i] = $names[$index];
		}
		$minprice = $car['cp'] - 3;
		$maxprice = $car['cp'] + 3;
		if($minprice < 0)
			$minprice = 0;
	    $mmprice = $minprice . '-' . $maxprice;
		$carModel = D('Common/Cars');
		$result = $carModel->car_list(0,6,session('mycity'),'d','','',$mmprice,'','','','','','','','','','','','','',session('ci'));
		$this->assign ( "imgnames", $imgnames );
		$this->assign ( "imgcount", $imgcount );
		$this->assign ( "recommend", $result['Data'] );
		$this->assign ( "car", $car );
		$this->display ();
	}
	function getcar($carid)
	{
		if(inject_check($carid))
			return null;
		$Model = new \Think\Model();
		$sql = "select c.id as id,
				ifnull(GROUP_CONCAT(m.id ORDER BY orderno ),'') as imgs,
				ifnull(GROUP_CONCAT(m.ImageType ORDER BY orderno ),'') as imgnames,
				trim(concat_ws(' ',if(locate(carbrand,carseries),'',carbrand),carseries,concat(caryear,'款') ,caremissions,carkind)) as na,
				ifnull(c.CarPrice,0) as cp,
				ifnull(c.CarNewPrice,0) as newcarprice,
				ifnull(carwantsale,0)  as cw,
				ifnull(carfullsale,0) as cf,
				if(TO_DAYS(CheckDate)=TO_DAYS(now()),1,0) as ct,
				if((now() - carlicdate < 365) and carrunlen < 2  ,1,0) as cn ,
				ROUND(CEIL(ifnull(c.CarPrice,0) * 0.3 * 10) / 10.00,2) as loanf,
				ROUND(CEIL((ifnull(c.CarPrice,0)  - CEIL((ifnull(c.CarPrice,0) * 0.3 * 10) / 10.00)) * 335.8),2) as loany,
				ROUND(case when c.CarPrice * 0.03 * 10000 > 2500 then c.CarPrice * 0.03 * 10000 else 2500 end,2) as svrprice,
				ifnull(carrunlen,0) as runlen, 
				ifnull(TIMESTAMPDIFF(MONTH, carlicdate,now()),0) as licmonth, 
				ifnull(date_format(carlicdate,'%Y年%m月'),'') as licdate,
				ifnull(c.CarLicPlace,'') as licplace,
				ifnull(c.CarEmKind,'') as emkind,
				ifnull(c.CarGearbox,'') as gearbox,
				ifnull(c.CarEmissions,0) as emissions,
				ifnull(c.CarLight,'') as light,
				ifnull(c.CarManSay,'') as mansay,
				ifnull(DATE_FORMAT(c.CarCheckOutDate,'%Y年%m月'),'') as chkoutdate,
				ifnull(DATE_FORMAT(c.CarMustInsOutDate,'%Y年%m月'),'') as mustinsoutdate,
				ifnull(DATE_FORMAT(c.CarInsOutDate,'%Y年%m月'),'') as insoutdate,
				ifnull(ViewNum + (TO_DAYS(NOW()) - TO_DAYS(c.CarCheckOutDate)) * 2,0) as viewnum,
				ifnull(CollectNum + (TO_DAYS(NOW()) - TO_DAYS(c.CarCheckOutDate)),0) as collectnum,
				c.cfid  as cfid,
				c.carno as carno,
				concat_ws('/',date_format(carlicdate,'%Y年'),concat_ws('',carrunlen,'万公里')) as ds
				from nc_cars c left join nc_carimage m on c.id=m.carid 
				where c.id='$carid'
				group by c.id;";
		$datas = $Model->query($sql);
		return $datas[0];
	}

	/**
	* 车辆详细信息
	*/
	public function carss()
	{
		$cfid = $_GET['cfid'];
		if(inject_check($cfid))
			return;
		$Model = new \Think\Model();
		$rls = $Model->query("select * from nc_CarConfig where id='$cfid'");
		if(count($rls) < 1)
		{
			echo "错误";
			return;
		}
		else
		{
			$this->assign ( "configlist", $rls[0] );
			$name = $_GET['na'];
			$this->assign ( "na", $name );
			$cp = $_GET['cp'];
			$this->assign ( "cp", $cp );
			$this->display ();
		}
	}

	/**
	*查看大图
	*/
	function detailbig()
	{
		$imgs = $_GET["imgs"];
		$carno = $_GET["carno"];
		$imgcount = count(explode(',',$imgs));
		$this->assign ( "imgs", $imgs );
		$this->assign ( "carno", $carno );
		$this->assign ( "imgcount", $imgcount );
		$this->display ();
	}


	/**
	*查看全部图片
	*/
	function detailall()
	{
		$imgs = $_GET["imgs"];
		$carno = $_GET["carno"];
		$this->assign ( "imgs", $imgs );
		$this->assign ( "carno", $carno );
		$this->display ();
	}
}